<?php
class Login extends cl_BasePage {

	public $indexsearchform;
	public $formlogin;
	
	function __construct() {
		parent::__construct();
		//parent::doInit();
		
		$this->title = "Login | CulturaLive.com | it_IT";
	}	
		
	
	function doInit() {
		parent::doInit();
		global $teardrop;
	
		$this->indexsearchform = new Header_IndexSearchform($teardrop->sysroot."/app/header/indexsearchform.phtml");
		$this->subheader->loginform = $this->indexsearchform;

		$this->createform();
		
		// Here it seems that on Carmine's PC (may be on other PHP installations...)
		// the session should not be started.
		// If the case we start it here.
		@session_start();
		
	}
	
	
	
	function createform() {
	
		global $webroot;
	
		$formlogin = new TdForm("form_login", "../core/app/form/form.phtml");
		$formlogin->setName("form_login");
		$formlogin->setMethod("POST");
		$formlogin->setAction($webroot."/login.htm");		
		
		            
		$fieldset1 = new TdFieldset("fieldset_signup1", "../core/app/form/fieldset.phtml");
	  	$fieldset1->setName("fieldset_signup1");
	  	$formlogin->addField($fieldset1);
	
		$hidden_login = new TdHiddenField("hidden_login", "../core/app/form/hiddenfield.phtml");
		$hidden_login->setValue("form_login");
		$hidden_login->setName("hidden_form");
		$formlogin->addField($hidden_login);
	
		$l_username = new TdLabel("label_username", "../core/app/form/label.phtml");
	  	$l_username->setClass("label_signup");
	  	$l_username->setValue("Username");
	
	  	$username = new TdTextField("username", "../core/app/form/textfield.phtml");
	  	$username->setLabel($l_username);
	  	$username->setRequired(true);
	  	$username->setName("username");
	  	$username->setSize("13");
	  	$username->setClass("input_signup");
	  	$fieldset1->addField($username);
	  	
	  	$l_password = new TdLabel("label_password", "../core/app/form/label.phtml");
	  	$l_password->setClass("label_signup");
	  	$l_password->setValue("Password");
			
		$password = new TdPasswordField("password", "../core/app/form/passwfield.phtml");
		$password->setRequired(true);
		$password->setLabel($l_password);		
		$password->setName("password");
		$password->setSize("13");
		$password->setClass("input_signup");		
		$fieldset1->addField($password);
		
	
	
		$submit = new TdSubmitField("submit_login", "../core/app/form/submit.phtml");
		$submit->setValue("");
		$submit->setName("login");
		$submit->setClass("submit_sign_up_1");
		$fieldset1->addField($submit);	
			
			
		$this->addComponent($formlogin);
		$this->formlogin = $formlogin;
	
	}
	
	
	function doValidate() {
	  global $teardrop;
	  
	  parent::doValidate();	  
	  
	  if (!$this->formlogin->gotError()) {
			$user = new cl_User("username", $this->formlogin->getField("fieldset_signup1")->getField("username")->getValue());
			
			// provvisorio
			if (!isset($_SESSION["user"])) {
				$_SESSION["user"] = $user;
			}
			
			//if ($this->formlogin->getField("fieldset_signup1")->getField("check_login")->getValue()) {
			if (isset($teardrop->arguments['check_login'])) {
				$teardrop->createcookie("username", $this->formlogin->getField("fieldset_signup1")->getField("username")->getValue(), time()+3600);
			}
			else {
				$teardrop->deletecookie("username");
			}
			
			//$teardrop->route("welcome.htm");
			
			//When logging in we redirect to the webroot (index.htm) and it routes to home.htm in turn
			$teardrop->redirect($teardrop->webroot);
			
		} else {
			
			
		}		

	}
	
	function logout() {
	  global $teardrop;
	  
	  $teardrop->deletecookie("username");
		unset($_SESSION["user"]);
		
		//$teardrop->route("index.htm");
		$teardrop->redirect($teardrop->webroot);
	}
	
}
?>
